package com.chance.cc.crawler.development.command.schedule.xcar;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2020/12/15 0015 13:26
 */
@Data
@AllArgsConstructor
public class XCarKeywordImport {

    public static void main(String[] args) throws IOException {
        String courseFile = System.getProperty("user.dir");
        courseFile += "/crawler-scripts-command/src/main/java/com/chance/cc/crawler/development/command/schedule/xcar/xcar_kw.txt";
        List<String> kwLines = IOUtils.readLines(new FileInputStream(courseFile), Charset.forName("UTF-8"));
        CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
                new CrawlerDomainKeywordCommand("192.168.1.217",9599);

        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")){
                continue;
            }
            String[] dyKeyword = kwLine.split("\t");


            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain("xcar");
            crawlerDomainKeys.setStatus(0);

            if (dyKeyword[1].equals("news")){
                Map<String,String> keyJson = new HashMap<>();
                keyJson.put("first_level_menu",dyKeyword[1]);
                keyJson.put("second_level_menu",dyKeyword[2]);
                keyJson.put("keyword_url",dyKeyword[3]);
                crawlerDomainKeys.setKeyword(JSON.toJSONString(keyJson));
                crawlerDomainKeys.setSite(dyKeyword[0]);
            } else if (dyKeyword[1].equals("carSeries")){
                crawlerDomainKeys.setKeyword(dyKeyword[0]);
                crawlerDomainKeys.setSite(dyKeyword[1]);
            }
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
            System.out.println(JSON.toJSONString(crawlerDomainKeys));
        }

    }
}
